package com.open.capacity.workflow3.service.impl;

import com.open.capacity.workflow3.dao.NwfTaskLogDao;
import com.open.capacity.workflow3.dao.NwfTaskRelationDao;
import com.open.capacity.workflow3.entity.NwfTaskLog;
import com.open.capacity.workflow3.entity.NwfTaskRelation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.workflow3.entity.NwfTask;
import com.open.capacity.workflow3.dao.NwfTaskDao;
import com.open.capacity.workflow3.service.NwfTaskService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class NwfTaskServiceImpl implements NwfTaskService {

    @Autowired
    private NwfTaskDao nwfTaskDao;
    @Autowired
    private NwfTaskLogDao taskLogDao;

    @Autowired
    private NwfTaskRelationDao taskRelationDao;

    public List<NwfTask> getALLTaskList(String processId) {
        return nwfTaskDao.findByProcessId(processId);
    }

    public List<NwfTask> getUnFinishTaskList(String processId) {
        Map<String, Object> param = new HashMap<>();
        param.put("processId", processId);
        param.put("isFinished", 0);
        return nwfTaskDao.findAll(param);
    }

    public List<NwfTask> getUnFinishTaskList() {
        Map<String, Object> param = new HashMap<>();
        param.put("taskTypes", new Integer[]{1, 3});
        param.put("isFinished", 0);
        return nwfTaskDao.findAll(param);
    }

    public boolean isRevokeTask(String processId, String preNodeId) {
        boolean res = true;

        Map<String, Object> param = new HashMap<>();
        param.put("processId", processId);
        param.put("preNodeId", preNodeId);
        param.put("taskTypes", new Integer[]{1, 5});
        param.put("isFinished", 0);
        int count = nwfTaskDao.count(param);
        if (count > 0) {
            return res;
        }

        param = new HashMap<>();
        param.put("processId", processId);
        param.put("prevNodeId", preNodeId);
        param.put("taskType", 1);
        param.put("isFinished", 1);
        List<NwfTask> list = nwfTaskDao.findAll(param);

        for (NwfTask item : list) {
            String nodeId = item.getNodeId();

            param = new HashMap<>();
            param.put("processId", processId);
            param.put("nodeId", nodeId);
            param.put("isFinished", 0);
            int c = nwfTaskDao.count(param);
            if (c == 0) {
                res = false;
                break;
            }
        }
        return res;
    }

    public NwfTask getEntity(String keyValue) {
        return nwfTaskDao.findById(keyValue);
    }

    public NwfTaskLog getLogEntityByNodeId(String nodeId, String prcoessId) {
        Map<String, Object> param = new HashMap<>();
        param.put("nodeId", nodeId);
        param.put("processId", prcoessId);
        param.put("noTaskTypes", new Integer[]{3, 6});
        List<NwfTaskLog> list = taskLogDao.findAll(param);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public NwfTaskLog getLogEntity(String taskId, String userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("taskId", taskId);
        param.put("createUserid", userId);
        param.put("noTaskType", 100);
        List<NwfTaskLog> list = taskLogDao.findAll(param);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public List<NwfTaskLog> getLogList(String processId) {
        Map<String, Object> param = new HashMap<>();
        param.put("processId", processId);
        List<NwfTaskLog> list = taskLogDao.findAll(param);

        return list;
    }

    public List<String> getCurrentNodeIds(String processId) {
        List<String> list = nwfTaskDao.getCurrentNodeIds(processId);
        return list;

    }

    public NwfTask getEntityByNodeId(String nodeId, String processId) {
        Map<String, Object> param = new HashMap<>();
        param.put("nodeId", nodeId);
        param.put("processId", processId);
        param.put("noTaskType", 3);
        List<NwfTask> list = nwfTaskDao.findAll(param);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public NwfTask getEntityByNodeId2(String nodeId, String processId) {
        Map<String, Object> param = new HashMap<>();
        param.put("nodeId", nodeId);
        param.put("processId", processId);
        param.put("taskType", 1);
        param.put("isFinished", 1);
        List<NwfTask> list = nwfTaskDao.findAll(param);

        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public List<NwfTaskRelation> getTaskUserList(String taskId) {
        List<NwfTaskRelation> list = taskRelationDao.findByTaskId(taskId);
        return list;
    }

    @Transactional
    public void save(List<NwfTaskRelation> list, List<String> taskList, NwfTaskLog nWFTaskLogEntity) {
        for (String taskId : taskList) {
            taskRelationDao.deleteByCondition(taskId, 0, 0);
        }
        for (NwfTaskRelation taskUser : list) {
            taskRelationDao.save(taskUser);
        }
        taskLogDao.save(nWFTaskLogEntity);
    }
}
